/**
*      author: Y_Yao
*      created: 2025-04-02 14:36:37
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 6e3 + 7;
int a[N];
vector<int> v[N];
bool vis[N];
int f[N][2];

void dfs(int x)
{
    f[x][0] = 0;//不选
    f[x][1] = a[x];//选
    for(int i = 0;i < v[x].size();i++)
    {
        int son = v[x][i];
        dfs(son);
        f[x][0] += max(f[son][0],f[son][1]);
        f[x][1] += f[son][0];
    }
}

void solve()
{
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
    }
    for(int i = 1;i <= n - 1;i++)
    {
        int l,k;
        cin >> l >> k;
        v[k].push_back(l);
        vis[l] = true;
    }
    int root = 0;
    for(int i = 1;i <= n;i++)
    {
        if(!vis[i])
        {
            root = i;
            break;
        }
    }
    dfs(root);
    cout << max(f[root][0],f[root][1]) << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}